#!/usr/bin/env bash
# @sys: centos-stream-8
# @date: Wed Jul  6 16:31:18 CST 2022
# @author: yangfan
# @email: yangfan@moonpac.com
# @info: install-redis
# @run: ./install-redis.sh 1 /opt



# vars
if [ ! -n "${1}" ] ;then
    echo -e "\033[31m Please enter parameter online (1) or offline (2) install --------> fail\033[0m"; exit 1;
else
    model=${1}
fi

if [ ! -n "${2}" ] ;then
    echo -e "\033[32mDefault redis version (6.2.6) --------> fail\033[0m";
    version=6.2.6
else
    version=${2}
fi

if [ ! -n "${3}" ] ;then
    echo -e "\033[32mDefault install dir '/opt' \033[0m";
    install_path=/opt
else
    install_path=${3}
fi

if [ ! -n "${4}" ] ;then
    echo -e "\033[32mDefault redis password 'Fhl3y_Jsdwj' \033[0m";
    redis_password=Fhl3y_Jsdwj
else
    redis_password=${4}
fi


# fun
function check() {
    if [[ $? == 0 ]]; then
        echo -e "\033[32m${1} --------> success\033[0m";
    else
        echo -e "\033[31m${1} --------> fail\033[0m";
        exit 1;
    fi
}

function wgetFun() {
    wget -c ${1} &>/dev/null
    check "wget ${1}"
}

function yumFun() {
    yum install ${*} -y &>/dev/null
    check "yum install ${*}"
}

function addUser() {
    id ${1} &>/dev/null
    if [[ $? != 0 ]]; then
        useradd -M -s /sbin/nologin ${1}
        check "add ${1} user"
    else
        id ${1}
    fi
}

function addFilewall() {
    status=`firewall-cmd --query-port=${1}`
    if [[ ${status} == no ]]; then
        firewall-cmd --add-port=${1} &>/dev/null
        firewall-cmd --add-port=${1} --permanent &>/dev/null
        check "firewall add ${1}"
    else
        echo "firewall add ${1}"
    fi
}

function startService() {
    systemctl enable ${1}.service --now &>/dev/null
    systemctl restart ${1}.service &>/dev/null
    check "restart ${1}"
}



# download redis
if [[ ${model} == 1 ]]; then
    wgetFun https://download.redis.io/releases/redis-${version}.tar.gz
elif [[ ${model} == 2 ]]; then
    if [[ ! -f redis-${version}.tar.gz ]]; then
        echo "source redis-${version}.tar.gz not exist"; exit 1;
    fi
fi

# unzip redis
tar xf redis-${version}.tar.gz && cd redis-${version} || exit 1;

# configure && make && make install redis
make -j12 &>/dev/null && check "make redis-${version}"
make PREFIX=${install_path}/redis-${version} install &>/dev/null && check "make install redis-${version}"

mkdir -p ${install_path}/redis-${version}/{conf,run}
cp -r redis.conf ${install_path}/redis-${version}/conf/redis.conf.bak
cp -r redis.conf ${install_path}/redis-${version}/conf/redis.conf
sed -i "/^bind 127.0.0.1 -::1/a bind 0.0.0.0" ${install_path}/redis-${version}/conf/redis.conf
sed -i "/^bind 127.0.0.1 -::1/s/^/#/" ${install_path}/redis-${version}/conf/redis.conf
sed -i "s/pidfile.*/pidfile \${install_path}\/redis\/run\/redis_6379.pid/" ${install_path}/redis-${version}/conf/redis.conf
sed -i "/^# requirepass foobared/a requirepass ${redis_password}" ${install_path}/redis-${version}/conf/redis.conf

cd .. && rm -rf redis-${version}

addFilewall 6379/tcp

cat > /lib/systemd/system/redis.service << END
[Unit]
Description=Redis
After=network.target

[Service]
Type=simple
PIDFile=${install_path}/redis-${version}/run/redis_6379.pid
ExecStart=${install_path}/redis-${version}/bin/redis-server ${install_path}/redis-${version}/conf/redis.conf
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s QUIT \$MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
END

startService redis
